﻿<%@ WebHandler Language="C#" Class="ShowListServiceCharge" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ShowListServiceCharge : AjaxHandlerBase {
    
 //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ：-
    //返回值説明     ：-
    //功能詳細       ：权限哈希表中添加操作与权限组
    //作成者         ：he.jifeng 
    //作成日         ：2010-09-14
    //*******************************************
    public ShowListServiceCharge()
    {
        //登记收据
        htTypePermit.Add("regcharge", Constants.Permit_RegServiceCharge);
        //作废登记
        htTypePermit.Add("cancel", Constants.Permit_CancelServiceCharge);
        //查看收据
        htTypePermit.Add("getpayitems", Constants.Permit_ShowServiceCharge);
        //登记合同
        htTypePermit.Add("regcontractcode", Constants.Permit_RegContractCode);
        //导出服务费
        htTypePermit.Add("export", Constants.Permit_ExportServiceCharge);
    }

    //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-09-14
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            
            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //获取信息
                case "getcontractinfo":
                    strRet = GetContractInfo(context);
                    break;
                //服务费收据列表
                case "getlist":
                    strRet = GetServiceChargeList(context);
                    break;
                //输入服务费收据
                case "regcharge":
                    strRet = RegServiceCharge(context);
                    break;
                //收据明细列表
                case "getpayitems":
                    strRet = GetChargePayItems(context);
                    break;
                //作废收据
                case "cancel":
                    strRet = CancelServiceCharge(context);
                    break;
                //登记成交编号
                case "regcontractcode":
                    strRet = RegContractCode(context);
                    break;
                default:
                    strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    //*******************************************
    //功能概要       ：获取合同信息
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：获取合同信息
    //作成者         ：he.jifeng
    //作成日         ：2010-09-15
    //*******************************************
    private string GetContractInfo(HttpContext context)
    {
        string strRet = string.Empty;
        //StringBuilder strHtml = new StringBuilder();
        string ContractCode = context.Request.Params["ContractCode"];
        ContractBaseInfo bllContract = new ContractBaseInfo();
        if (string.IsNullOrEmpty(ContractCode))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            Chenxin.Lib.Business.Entity.t_Contract_BaseInfo entContract = bllContract.GetModelByCode(ContractCode);
            if (entContract == null)
            {
                strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_ContractCodeInvalid, "");
            }
            else
            {
                strRet = retExecResult(Constants.ExecResult.Succ, "", entContract);
            }
        }
        
        return strRet;
    }
    
    //*******************************************
    //功能概要       ：获取服务费收据列表
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：获取服务费收据列表
    //作成者         ：he.jifeng
    //作成日         ：2010-09-14
    //*******************************************
    private string GetServiceChargeList(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();
        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ServiceChargeBaseInfo bllBaseInfo = new ServiceChargeBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }
        strParam = context.Request.Params["Keyword"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Keyword", strParam);
        }
        strParam = context.Request.Params["Status"];
        
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Status", strParam);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["userid"]))
        {
            dicParams.Add("UserId", context.Request.Params["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllServiceCharge))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityServiceCharge))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyServiceCharge))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        //查看部门
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewDepartServiceCharge))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }
        
        

        DataSet dsBaseInfo = bllBaseInfo.GetListByConditions(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", Chenxin.Lib.Utility.JsonHelp.Convert2Json(dsBaseInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBaseInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();
        return strRet;
    }
    
    //*******************************************
    //功能概要       ：输入服务费收据
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：输入服务费收据
    //作成者         ：he.jifeng
    //作成日         ：2010-09-14
    //*******************************************
    private string RegServiceCharge(HttpContext context)
    {
        string strRet = string.Empty;
        string clientModel = context.Request.Params["model"];
        string PayItems = context.Request.Params["PayItems"];

        ServiceChargeBaseInfo bllBaseInfo = new ServiceChargeBaseInfo();
        ServiceChargePaymodeInfo bllModeInfo = new ServiceChargePaymodeInfo();
        Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entBaseInfo = (Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo)JsonHelp.Convert2Entity(clientModel, typeof(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo));
        List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo> lsModeInfo = (List < Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo >) JsonHelp.Convert2Entity(PayItems, typeof(List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo>));

        if (entBaseInfo == null)
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            if (!ChkContractCode(entBaseInfo))
            {
                strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_ContractCodeInvalid, "");
            }
            else
            {
                entBaseInfo.ReceiptId = Guid.NewGuid();
                entBaseInfo.ReceiptCode = bllBaseInfo.GenerateCode(CurrentUserInfo.CompanyId);
                entBaseInfo.AddEmployeeId = CurrentUserInfo.UserId;
                entBaseInfo.AddEmployee = CurrentUserInfo.UserName;
                entBaseInfo.AddTime = DateTime.Now;
                entBaseInfo.Status = 1;
                bllBaseInfo.Add(entBaseInfo);

                for (int idxCnt = 0; idxCnt < lsModeInfo.Count; idxCnt++)
                {
                    lsModeInfo[idxCnt].ReceiptId = entBaseInfo.ReceiptId;
                    lsModeInfo[idxCnt].RecordId = Guid.NewGuid();
                    bllModeInfo.Add(lsModeInfo[idxCnt]);
                }
                
                //如有合同编号，则去掉核销
                if (!string.IsNullOrEmpty(entBaseInfo.ContractCode))
                {
                    Chenxin.Lib.Business.Entity.t_Contract_BaseInfo info = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModelByCode(entBaseInfo.ContractCode);
                    info.HasOver = false;
                    info.OverName = "";
                    info.OverTime = null;
                    Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.Update(info);
                }

                //****************获取打印信息****************************
                Chenxin.Lib.Business.Entity.v_PrintServiceInfo model = Chenxin.Lib.Business.DAL.v_PrintServiceInfo.GetModel(entBaseInfo.ReceiptId);
                if (model == null) return retExecResult(Constants.ExecResult.Error, "获取服务费信息失败，请联系管理员！", "");

                model.PrintTime = DateTime.Now.ToShortDateString();
                string str = "";
                Chenxin.Lib.Utility.InitialHelper.TranslateNumberToChinese(model.GetedMoney.ToString(), out str);
                model.GetedMoneyBig = str;

                string strSql = "IntentionId='" + entBaseInfo.ReceiptId + "'";

                List<Chenxin.Lib.Business.Entity.t_Intention_BillPrintInfo> list = Chenxin.Lib.Business.DAL.t_Intention_BillPrintInfo.GetListArray(strSql);
                model.PrintCount = list.Count + 1;

                //获得服务费款项List        
                StringBuilder sb = new StringBuilder();

                List<Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo> itemslist = Chenxin.Lib.Business.DAL.ServiceChargePaymodeInfo.GetListArray(" ReceiptId='" + model.ReceiptId + "'");
                sb.Append("<table width=\"100%\" cellspacing=\"1\" style=\"margin-left:15px;\" class=\"tabsorternew table_archive\">");
                sb.Append("<tr>");
                sb.AppendFormat("<th style=\"text-align:center;width:15%;font-size:13px;\">收取方式</th><th style=\"text-align:center;width:15%;font-size:13px;\">金额</th><th style=\"text-align:center;width:15%;font-size:13px;\">交款编号</th><th style=\"text-align:center;font-size:13px;width:15%;\">备注</th>");
                sb.Append("</tr>");

                if (itemslist.Count < 1)
                {
                    sb.Append("<tr><td colspan=\"4\" style=\"text-align:center;height: 25px;font-size:13px;\">无款项明细记录</td></tr>");
                }
                else
                {
                    foreach (Chenxin.Lib.Business.Entity.ServiceChargePaymodeInfo item in itemslist)
                    {
                        sb.Append("<tr>");
                        sb.AppendFormat("<td style=\"text-align:center;font-size:13px;height: 25px;\">{0}</td><td style=\"text-align:center;font-size:13px;\">{1}</td><td style=\"text-align:center;font-size:13px;\">{2}</td><td style=\"text-align:center;font-size:13px;\">{3}</td>", item.PayMode, item.PayMoney, item.PayCode, item.Memo);
                        sb.Append("</tr>");
                    }
                }
                sb.Append("</table>");

                model.Details = sb.ToString();

                strRet = string.Format("{{success:1,msg:'服务费登记成功！',data:{{model:{0}}}}}", JsonHelp.Convert2Json(model));
                //**********************************************************
                //strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_RegServiceChargeSucc, "");
            }
        }
        
        return strRet;
    }

    //*******************************************
    //功能概要       ：获取收据明细
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：获取收据明细
    //作成者         ：he.jifeng
    //作成日         ：2010-09-15
    //*******************************************
    private string GetChargePayItems(HttpContext context)
    {
        string strRet = string.Empty;
        StringBuilder strHtml = new StringBuilder();
        string ReceiptId = context.Request.Params["ReceiptId"];

        if (string.IsNullOrEmpty(ReceiptId))
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            ServiceChargePaymodeInfo bllPayInfo = new ServiceChargePaymodeInfo();
            DataSet dsPayInfo = bllPayInfo.GetListByReceiptId(ReceiptId);

            strHtml.Append("{");
            strHtml.Append("success:'1',");
            strHtml.Append("msg:'',");
            strHtml.AppendFormat("data:{{{0}", Chenxin.Lib.Utility.JsonHelp.Convert2Json(dsPayInfo.Tables[0]));
            strHtml.Append("}");
            strHtml.Append("}");

            strRet = strHtml.ToString(); 
        }
        
        return strRet;
    }
    
    //*******************************************
    //功能概要       ：检查合同号
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：检查合同号
    //作成者         ：he.jifeng
    //作成日         ：2010-09-15
    //*******************************************
    private bool ChkContractCode(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entBaseInfo)
    {
        bool bRet = true;
        ContractBaseInfo bllContract = new ContractBaseInfo();
        if (string.IsNullOrEmpty(entBaseInfo.ContractCode))
        {
            bRet = true;
        }
        else
        {
            Chenxin.Lib.Business.Entity.t_Contract_BaseInfo entContract = bllContract.GetModelByCode(entBaseInfo.ContractCode);

            if (entContract == null)
            {
                bRet = false;
            }
            else
            {
                entBaseInfo.ContractId = entContract.ContractId;
                bRet = true;
            }
        }
        return bRet;
    }
    //*******************************************
    //功能概要       ：作废服务费收据
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：作废服务费收据
    //作成者         ：he.jifeng
    //作成日         ：2010-09-14
    //*******************************************
    private string CancelServiceCharge(HttpContext context)
    {
        string strRet = string.Empty;
        string clientModel = context.Request.Params["model"];
        ServiceChargeBaseInfo bllBaseInfo = new ServiceChargeBaseInfo();
        Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entBaseInfo = (Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo)JsonHelp.Convert2Entity(clientModel, typeof(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo));
        if (entBaseInfo == null)
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entUpdBaseInfo = bllBaseInfo.GetModel(entBaseInfo.ReceiptId);
            entUpdBaseInfo.UnusedDate = DateTime.Now;
            entUpdBaseInfo.UnusedPerson = entBaseInfo.UnusedPerson;
            entUpdBaseInfo.UnusedPersonId = entBaseInfo.UnusedPersonId;
            entUpdBaseInfo.UnusedReason = entBaseInfo.UnusedReason;
            entUpdBaseInfo.Status = -1;

            bllBaseInfo.Update(entUpdBaseInfo);
            strRet = retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_CancelServiceChargeSucc, "");
        }
        
        return strRet;
    }
    
    //*******************************************
    //功能概要       ：登记成交编号
    //参数説明       ：context 请求上下文
    //返回值説明     ：执行结果
    //功能詳細       ：登记成交编号
    //作成者         ：he.jifeng
    //作成日         ：2010-09-16
    //*******************************************
    private string RegContractCode(HttpContext context)
    {
        string strRet = string.Empty;
        string clientModel = context.Request.Params["model"];
        ServiceChargeBaseInfo bllBaseInfo = new ServiceChargeBaseInfo();
        Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entBaseInfo = (Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo)JsonHelp.Convert2Entity(clientModel, typeof(Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo));
        if (entBaseInfo == null)
        {
            strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_UrlError, "");
        }
        else
        {
            if (!ChkContractCode(entBaseInfo))
            {
                strRet = retExecResult(Constants.ExecResult.Error, Resources.Message.Err_ContractCodeInvalid, "");
            }
            else
            {
                Chenxin.Lib.Business.Entity.ServiceChargeBaseInfo entUpdBaseInfo = bllBaseInfo.GetModel(entBaseInfo.ReceiptId);
                entUpdBaseInfo.ContractCode = entBaseInfo.ContractCode;
                entUpdBaseInfo.ContractId = entBaseInfo.ContractId;

                bllBaseInfo.Update(entUpdBaseInfo);
                strRet = retExecResult(Constants.ExecResult.Succ, "成交编号登记成功！", "");
            }
        }
        return strRet;
    }
    //*******************************************
    //功能概要       ：自定义权限检查
    //参数説明       ：strMsg 消息 
    //返回值説明     ：有无权限
    //功能詳細       ：自定义权限检查
    //作成者         ：he.jifeng
    //作成日         ：2010-09-16
    //*******************************************
    public override bool CustomerCheckPermint(HttpContext context, out string strMsg)
    {
        string op = context.Request["op"].ToLower();
        if (op != null)
        {
            if (op.Equals("checkpermit"))
            {
                string Code = context.Request.Params["code"];
                switch (Code)
                {
                    //服务费收据登记
                    case "regcharge":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_RegServiceCharge))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //作废服务费收据
                    case "cancel":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_CancelServiceCharge))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    //登记成交编号
                    case "regcontractcode":
                        if (!CurrentUserInfo.ContainsPermit(Constants.Permit_RegContractCode))
                        {
                            strMsg = "{success:1,msg:'',data:{permit:false}}";
                            return true;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        strMsg = "{success:1,msg:'',data:{permit:true}}";
        return true;
    }
}